package me.goldze.mvvmhabit.net;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;


import com.hjq.toast.ToastUtils;

import java.io.IOException;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import me.goldze.mvvmhabit.base.BaseActivity;
import me.goldze.mvvmhabit.lks.router.RouteHelper;
import me.goldze.mvvmhabit.utils.GsonUtils;
import me.goldze.mvvmhabit.utils.SPUtils;
import me.goldze.mvvmhabit.base.AppManager;
import retrofit2.adapter.rxjava2.HttpException;


/**
 * LkSSubscriber
 */
public abstract class LkSSubscriber<T> implements Observer<T> {

    public LkSSubscriber() {
    }

    @Override
    public void onSubscribe(Disposable d) {

    }

    @Override
    public void onNext(T t) {
        LKSResponse lksResponse = null;
        try {
            lksResponse = (LKSResponse) t;
        } catch (Exception e) {
            if (null == lksResponse) {
                throw new IllegalArgumentException("传入的类型不匹配！请传入LKSResponse");
            }
        }
        try {
            if (lksResponse.getState() == 0) {
                if (lksResponse.getMessage().equals("未进行实名")) {
                    ((BaseActivity) (AppManager.getAppManager().currentActivity())).showAuthDialog();
                    return;
                }

                if (null == lksResponse.getData() || TextUtils.isEmpty(lksResponse.getData().toString())) {
                    onSuccess("");
                } else {
                    if (lksResponse.getData() instanceof String) {
                        onSuccess(lksResponse.getData().toString());
                    } else if (lksResponse.getData() instanceof Integer) {
                        onSuccess(lksResponse.getData().toString() + "");
                    } else {
                        onSuccess(GsonUtils.getJsonString(lksResponse.getData()));
                    }
                }
            } else if (lksResponse.getState() == 302) {


            } else {
                onFail(lksResponse.getMessage());
            }
        } catch (Exception e) {
            onError(e);
        }

    }

    @Override
    public void onError(Throwable e) {
        e.printStackTrace();
        try {
            if (e instanceof ExceptionHandle.ResponeThrowable) {

                onError((ExceptionHandle.ResponeThrowable) e);
            } else if (e instanceof HttpException) {
                if(((HttpException) e).code()==302){
                    //                TODO 登录
                    if (!AppManager.getAppManager().currentActivity().getLocalClassName().equals("com.cn.user.ui.login.ActLogin")) {
                        Log.e("beuce", "currentActivity>>>>>: " + AppManager.getAppManager().currentActivity().getLocalClassName());
                        Context context = AppManager.getAppManager().currentActivity();
                        Intent intent = new Intent();
                        intent.putExtra(RouteHelper.ROUTE_KEY, "user_ActLogin");
                        RouteHelper.openActivity(intent, context, 0);
                    }
                    return;
                }

                try {
                    String string = ((HttpException) e).response().errorBody().string();
                    if (TextUtils.isEmpty(string)) {
//                        onFail("网络错误");
                        return;
                    }
                    String message = GsonUtils.getValueByName(string, "message");
                    int code = GsonUtils.getIntValue(string, "state");
                    onFail(code);
                    onFail(message);
                } catch (IOException ex) {
                    ex.printStackTrace();
                    ToastUtils.show("网络链接错误！");
                }
                onError(new ExceptionHandle.ResponeThrowable(e, ExceptionHandle.ERROR.UNKNOWN));
            } else {
                onError(new ExceptionHandle.ResponeThrowable(e, ExceptionHandle.ERROR.UNKNOWN));
                ToastUtils.show("网络链接错误！");
            }
        } catch (Exception ex) {

        }

    }


    @Override
    public void onComplete() {

    }

    public void onTokenFail() {

    }

    public void onFail(int errCode) {

    }

    /**
     * 连接错误
     *
     * @param e
     */
    public abstract void onError(ExceptionHandle.ResponeThrowable e);

    /**
     * 服务器错误
     *
     * @param s
     */
    public abstract void onFail(String s);

    /**
     * 成功
     *
     * @param result
     */
    public abstract void onSuccess(String result);

}
